package ar.com.nexttome.tasks;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
import ar.com.nexttome.MainActivity;
import ar.com.nexttome.PersonaManager;
import ar.com.nexttome.context.ApplicationContext;
import ar.com.nexttome.entities.Persona;
import ar.com.nexttome.util.Constantes;

public class MiPosicionTask extends AsyncTask<String, Void, Location> implements LocationListener {
	private LocationManager locationManager;
	private String bestProvider;
	private MainActivity activity;

	public MiPosicionTask(MainActivity activity, String locationService) {
		super();
		this.activity = activity;
	}

	protected void onPreExecute() {
		locationManager = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		bestProvider = locationManager.getBestProvider(criteria, false);
		locationManager.requestLocationUpdates(bestProvider, 20000, 1, this);
	}

	protected Location doInBackground(String... params) {	

		if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			Toast.makeText(activity, Constantes.STR_VOLVER_GPS, Toast.LENGTH_SHORT).show();
			activity.startActivity(gpsOptionsIntent);
		}

		Location location = locationManager.getLastKnownLocation(bestProvider);

		return location;
	}

	protected void onResume() {
		locationManager.requestLocationUpdates(bestProvider, 20000, 1, this);
	}

	protected void onPostExecute(Location location) {
		Persona yo = ((ApplicationContext) activity.getApplicationContext()).getCurrentUser();
		double lat = 0;
		double lon = 0;

		if ( location != null ) {
			lat = location.getLatitude();
			lon = location.getLongitude();
		}

		yo.setLatitud(lat);
		yo.setLongitud(lon);

		Toast.makeText(activity, Constantes.STR_ACT_POSICION + lat+"-"+lon, Toast.LENGTH_SHORT).show();

		try {
			PersonaManager.guardarPerfil(yo);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void onLocationChanged(Location arg0) {
		Persona yo = ((ApplicationContext) activity.getApplicationContext()).getCurrentUser();
		double lat = 0;
		double lon = 0;

		if ( arg0 != null ) {
			lat = arg0.getLatitude();
			lon = arg0.getLongitude();
		}

		yo.setLatitud(lat);
		yo.setLongitud(lon);

		try {
			PersonaManager.guardarPerfil(yo);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void onProviderDisabled(String arg0) {
	}

	public void onProviderEnabled(String arg0) {
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	}
}